package com.warm.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.warm.core.constant.TenantConstants;
import com.warm.core.domain.R;
import com.warm.core.page.BaseController;
import com.warm.core.page.TableDataInfo;
import com.warm.core.validate.AddGroup;
import com.warm.core.validate.EditGroup;
import com.warm.excel.poi.utils.ExcelUtil;
import com.warm.log.annotation.Log;
import com.warm.log.enums.BusinessType;
import com.warm.orm.domain.sys.SysTenant;
import com.warm.system.service.ISysTenantService;
import com.warm.tenant.helper.TenantHelper;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.validation.annotation.*;

import java.util.List;

/**
 * 租户Controller
 *
 * @author hh
 * @date 2023-10-27
 */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@Valid
@Controller
@Mapping("/system/tenant")
public class SysTenantController extends BaseController {
    @Inject
    private ISysTenantService tenantService;

    /**
     * 分页查询租户列表
     */
    @SaCheckPermission("system:tenant:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysTenant> list(SysTenant sysTenant) {
        return tenantService.selectPage(sysTenant);
    }

    /**
     * 导出租户列表
     */
    @SaCheckPermission("system:tenant:export")
    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @Post
    @Mapping("/export")
    public void export(Context ctx, SysTenant sysTenant) {
        List<SysTenant> list = tenantService.selectList(sysTenant);
        ExcelUtil<SysTenant> util = new ExcelUtil<>(SysTenant.class);
        util.exportExcel(ctx, list, "租户数据");
    }

    /**
     * 获取租户详细信息
     */
    @SaCheckPermission("system:tenant:query")
    @Get
    @Mapping(value = "/{id}")
    public R<SysTenant> getInfo(Long id) {
        return R.ok(tenantService.selectById(id));
    }

    /**
     * 新增租户
     */
    @SaCheckPermission("system:tenant:add")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @Post
    @Mapping
    public R<Void> add(@Validated(AddGroup.class) SysTenant sysTenant) {
        if (!tenantService.checkCompanyNameUnique(sysTenant)) {
            return R.fail("新增租户'" + sysTenant.getCompanyName() + "'失败，企业名称已存在", null);
        }
        return toR(tenantService.insert(sysTenant));
    }

    /**
     * 修改租户
     */
    @SaCheckPermission("system:tenant:edit")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @Put
    @Mapping
    public R<Void> edit(@Validated(EditGroup.class) SysTenant sysTenant) {
        tenantService.checkTenantAllowed(sysTenant.getTenantId());
        if (!tenantService.checkCompanyNameUnique(sysTenant)) {
            return R.fail("修改租户'" + sysTenant.getCompanyName() + "'失败，公司名称已存在", null);
        }
        return toR(tenantService.update(sysTenant));
    }

    /**
     * 删除租户
     */
    @SaCheckPermission("system:tenant:remove")
    @Log(title = "租户", businessType = BusinessType.DELETE)
	@Delete
    @Mapping("/{ids}")
    public R<Void> remove(Long[] ids) {
        return toR(tenantService.deleteByIds(ids));
    }

    /**
     * 修改租户状态
     */
    @SaCheckPermission("system:tenantPackage:remove")
    @Log(title = "租户套餐", businessType = BusinessType.DELETE)
    @Put
    @Mapping("changeStatus")
    public R<Void> changeStatus(SysTenant sysTenant) {
        return toR(tenantService.changeStatus(sysTenant));
    }

    /**
     * 动态切换租户
     *
     * @param tenantId 租户ID
     */
    @Get
    @Mapping("/dynamic/{tenantId}")
    public R<Void> dynamicTenant(@NotBlank(message = "租户ID不能为空") String tenantId) {
        TenantHelper.setDynamic(tenantId);
        return R.ok();
    }

    /**
     * 清除动态租户
     */
    @Get
    @Mapping("/dynamic/clear")
    public R<Void> dynamicClear() {
        TenantHelper.clearDynamic();
        return R.ok();
    }

    /**
     * 同步租户套餐
     *
     * @param tenantId  租户id
     * @param packageId 套餐id
     */
    @SaCheckPermission("system:tenant:edit")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @Get
    @Mapping("/syncTenantPackage")
    public R<Void> syncTenantPackage(@NotBlank(message = "租户ID不能为空") String tenantId,
                                     @NotNull(message = "套餐ID不能为空") Long packageId) {
        return toR(tenantService.syncTenantPackage(tenantId, packageId));
    }

}
